﻿using System;
using Terraria.ModLoader.Exceptions;

namespace Terraria.ModLoader
{
	// Token: 0x02000200 RID: 512
	public class RecipeEditor
	{
		// Token: 0x060016B5 RID: 5813 RVA: 0x0001156B File Offset: 0x0000F76B
		public RecipeEditor(Recipe recipe)
		{
			this.recipe = recipe;
		}

		// Token: 0x060016B6 RID: 5814 RVA: 0x00415D78 File Offset: 0x00413F78
		public void AddIngredient(int itemID, int stack = 1)
		{
			if (itemID <= 0 || itemID >= ItemLoader.ItemCount)
			{
				throw new RecipeException("No item has ID " + itemID);
			}
			for (int i = 0; i < Recipe.maxRequirements; i++)
			{
				if (this.recipe.requiredItem[i].type == 0)
				{
					this.recipe.requiredItem[i].SetDefaults(itemID, false);
					this.recipe.requiredItem[i].stack = stack;
					return;
				}
				if (this.recipe.requiredItem[i].type == itemID)
				{
					this.recipe.requiredItem[i].stack += stack;
					return;
				}
			}
			throw new RecipeException("Recipe already has maximum number of ingredients");
		}

		// Token: 0x060016B7 RID: 5815 RVA: 0x00415E30 File Offset: 0x00414030
		public bool SetIngredientStack(int itemID, int stack)
		{
			if (itemID <= 0 || itemID >= ItemLoader.ItemCount)
			{
				throw new RecipeException("No item has ID " + itemID);
			}
			for (int i = 0; i < Recipe.maxRequirements; i++)
			{
				if (this.recipe.requiredItem[i].type == itemID)
				{
					this.recipe.requiredItem[i].stack = stack;
					return true;
				}
			}
			return false;
		}

		// Token: 0x060016B8 RID: 5816 RVA: 0x00415E9C File Offset: 0x0041409C
		public bool DeleteIngredient(int itemID)
		{
			if (itemID <= 0 || itemID >= ItemLoader.ItemCount)
			{
				throw new RecipeException("No item has ID " + itemID);
			}
			for (int i = 0; i < Recipe.maxRequirements; i++)
			{
				if (this.recipe.requiredItem[i].type == itemID)
				{
					for (int j = i; j < Recipe.maxRequirements - 1; j++)
					{
						this.recipe.requiredItem[j] = this.recipe.requiredItem[j + 1];
					}
					this.recipe.requiredItem[Recipe.maxRequirements - 1] = new Item();
					return true;
				}
			}
			return false;
		}

		// Token: 0x060016B9 RID: 5817 RVA: 0x00415F3C File Offset: 0x0041413C
		public bool AcceptRecipeGroup(string groupName)
		{
			int item;
			if (!RecipeGroup.recipeGroupIDs.TryGetValue(groupName, out item))
			{
				throw new RecipeException("No recipe group is named " + groupName);
			}
			if (this.recipe.acceptedGroups.Contains(item))
			{
				return false;
			}
			this.recipe.acceptedGroups.Add(item);
			return true;
		}

		// Token: 0x060016BA RID: 5818 RVA: 0x00415F90 File Offset: 0x00414190
		public bool RejectRecipeGroup(string groupName)
		{
			int item;
			if (!RecipeGroup.recipeGroupIDs.TryGetValue(groupName, out item))
			{
				throw new RecipeException("No recipe group is named " + groupName);
			}
			return this.recipe.acceptedGroups.Remove(item);
		}

		// Token: 0x060016BB RID: 5819 RVA: 0x00415FD0 File Offset: 0x004141D0
		public void SetResult(int itemID, int stack = 1)
		{
			if (itemID <= 0 || itemID >= ItemLoader.ItemCount)
			{
				throw new RecipeException("No item has ID " + itemID);
			}
			this.recipe.createItem.SetDefaults(itemID, false);
			this.recipe.createItem.stack = stack;
		}

		// Token: 0x060016BC RID: 5820 RVA: 0x00416024 File Offset: 0x00414224
		public bool AddTile(int tileID)
		{
			if (tileID < 0 || tileID >= TileLoader.TileCount)
			{
				throw new RecipeException("No tile has ID " + tileID);
			}
			for (int i = 0; i < Recipe.maxRequirements; i++)
			{
				if (this.recipe.requiredTile[i] == -1)
				{
					this.recipe.requiredTile[i] = tileID;
					return true;
				}
				if (this.recipe.requiredTile[i] == tileID)
				{
					return false;
				}
			}
			throw new RecipeException("Recipe already has maximum number of tiles");
		}

		// Token: 0x060016BD RID: 5821 RVA: 0x004160A0 File Offset: 0x004142A0
		public bool DeleteTile(int tileID)
		{
			if (tileID < 0 || tileID >= TileLoader.TileCount)
			{
				throw new RecipeException("No tile has ID " + tileID);
			}
			for (int i = 0; i < Recipe.maxRequirements; i++)
			{
				if (this.recipe.requiredTile[i] == tileID)
				{
					for (int j = i; j < Recipe.maxRequirements - 1; j++)
					{
						this.recipe.requiredTile[j] = this.recipe.requiredTile[j + 1];
					}
					this.recipe.requiredTile[Recipe.maxRequirements - 1] = -1;
					return true;
				}
			}
			return false;
		}

		// Token: 0x060016BE RID: 5822 RVA: 0x0001157A File Offset: 0x0000F77A
		public void SetNeedWater(bool needWater)
		{
			this.recipe.needWater = needWater;
		}

		// Token: 0x060016BF RID: 5823 RVA: 0x00011588 File Offset: 0x0000F788
		public void SetNeedLava(bool needLava)
		{
			this.recipe.needLava = needLava;
		}

		// Token: 0x060016C0 RID: 5824 RVA: 0x00011596 File Offset: 0x0000F796
		public void SetNeedHoney(bool needHoney)
		{
			this.recipe.needHoney = needHoney;
		}

		// Token: 0x060016C1 RID: 5825 RVA: 0x00416134 File Offset: 0x00414334
		public bool DeleteRecipe()
		{
			for (int i = 0; i < Recipe.numRecipes; i++)
			{
				if (Main.recipe[i] == this.recipe)
				{
					for (int j = i; j < Recipe.numRecipes - 1; j++)
					{
						Main.recipe[j] = Main.recipe[j + 1];
					}
					Main.recipe[Recipe.numRecipes - 1] = new Recipe();
					Recipe.numRecipes--;
					return true;
				}
			}
			return false;
		}

		// Token: 0x040015A4 RID: 5540
		private Recipe recipe;
	}
}
